<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button type="primary" size="small" @click="btn">+新增角色</el-button>
          <el-table :data="list">
            <el-table-column label="序号" type="index">
              <template v-slot="{ $index }">
                <div>
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="角色名" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button type="text" @click="SetPermission(row)">分配权限</el-button>
                  <el-button type="text" @click="edit(row)">修改</el-button>
                  <!-- 删除,传递当前行数的数据 -->
                  <el-button type="text" @click="del(row)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="page">
            <!-- <el-pagination
            :current-page="page.page"  默认页码
            :page-sizes="[1, 3, 10, 20]"  页容量选项
            :page-size="page.pagesize"  默认页容量
            layout="total---总数, sizes---页容量, prev---上一页, pager页码, next---下一页, jumper---跳转到哪一页"
            :total="page.total" 总数
            @size-change="handleSizeChange"页容器改变
            @current-change="handleCurrentChange" 页码改变
          /> -->
            <el-pagination
              :current-page="page.page"
              :page-sizes="[1, 3, 10, 20]"
              :page-size="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息">
          <Info />
        </el-tab-pane>
      </el-tabs>
      <add ref="add" @getDate="getDate" />
      <!-- 导入分配权限 -->
      <SetPermission ref="SetPermission" :isShow.sync="ShowPermission" />
    </el-card>
  </div>
</template>

<script>
// 导入数据
import { sysRole, sysRoleDelete } from '@/api/setting.js'
// 导入分配权限
import SetPermission from './components/setPermission.vue'
export default {
  components: {
    Info: () => import('./components/info.vue'),
    add: () => import('./components/add.vue'),
    SetPermission
  },
  data() {
    return {
      ShowPermission: false,
      show: false,
      list: [],
      page: {
        page: 1,
        pagesize: 10,
        total: 1
      }
    }
  },
  created() {
    this.getDate()
  },
  methods: {
    // 页面容量的改变
    handleSizeChange(pagesize) {
      // console.log(size)
      this.page.pagesize = pagesize
      this.page.page = 1
      this.getDate()
    },
    handleCurrentChange(page) {
      // 页码改变
      this.page.page = page
      this.getDate()
    },
    async getDate() {
      const res = await sysRole(this.page)
      console.log(res, 888)
      // 把数据给list数组
      this.list = res.data.rows
      // 总条数
      this.page.total = res.data.total
    },
    btn() {
      // 显示表单
      // this.show = true
      this.$refs.add.isShow = true
      this.$refs.add.mode = 'add'
    },
    del(row) {
      console.log(row)
      this.$confirm('你确定要删除啊', '提示')
        .then(async() => {
          await sysRoleDelete(row.id)
          this.$message.success('删除成功')
          // this.getDate()
          this.handleCurrentChange(1)
        })
        .catch(() => {})
    },
    edit(row) {
      this.$refs.add.isShow = true
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
      this.$refs.add.mode = 'edit'
    },
    SetPermission(row) {
      this.ShowPermission = true
      this.$refs.SetPermission.getRoleInfo(row)
    }
  }

}
</script>

<style>

</style>
